import { defineStore } from 'pinia'
import axios from '../api/request'
import { AxiosResp, User } from '../model/Model'

export const useUserInfo = defineStore('userInfo', {
    state: () => {
        return {
            token: '',
            user: {} as User
        }
    },
    getters: {
        getterToken: (state) => state.token,
        getterUser: (state) => state.user
    },
    actions: {
        getUserInfo(token: string) {
            axios.get('/users').then((resp: AxiosResp<User>) => {
                this.user = resp.data.data
            })
        },
        clearUserInfo() {
            this.token = ''
            this.user = {} as User
        }
    },
    persist: {
        key: 'current_userInfo',
        storage: localStorage
    }
})